/**
 * 
 */
package edu.pku.sei.modeler.uml.commands.statemachine;

import org.eclipse.draw2d.geometry.Rectangle;

import edu.pku.sei.modeler.model.core.MetaModelElement;
import edu.pku.sei.modeler.model.structure.MetaNode;
import edu.pku.sei.modeler.model.uml.BehaviorStateMachines.Pseudostate;
import edu.pku.sei.modeler.model.uml.BehaviorStateMachines.State;
import edu.pku.sei.modeler.model.uml.BehaviorStateMachines.StateMachine;
import edu.pku.sei.modeler.uml.commands.NestedElementCreateCommand;

/**
 * @author YanYan
 * 
 */
public class NestedStateCreateCommand extends NestedElementCreateCommand {

	public NestedStateCreateCommand(MetaNode node, MetaNode hostNode,
			Rectangle constraint) {
		super(node, hostNode, constraint);
	}
	
	@Override
	public boolean canExecute() {
		if (node.getModelElement() instanceof Pseudostate
				|| node.getModelElement() instanceof State)
			return super.canExecute();
		else return false;
	}
	
	@Override
	protected void updateModelElement() {
		MetaModelElement modelElement = this.node.getModelElement();
		if (fatherNode.getModelElement() instanceof State) {
			State parentState = (State) fatherNode.getModelElement();
			parentState.getOwnedMember().add(modelElement);
		} else if (fatherNode.getModelElement() instanceof StateMachine) {
			StateMachine stateMachine = (StateMachine) this.node.getModelElement();
			stateMachine.getConnectionPoint().add(modelElement);
		}
	}

	@Override
	protected void undoModelElement() {
		MetaModelElement modelElement = this.node.getModelElement();
		if (fatherNode.getModelElement() instanceof State) {
			State parentState = (State) fatherNode.getModelElement();
			parentState.getOwnedMember().remove(modelElement);
		} else if (fatherNode.getModelElement() instanceof Pseudostate) {
			StateMachine stateMachine = (StateMachine) this.node.getModelElement();
			stateMachine.getConnectionPoint().remove(modelElement);
		}

	}

}
